module service.cate_service;

import d2sqlite3;
import model.category;
import utils.const_val;

/** 
 * 获取所有分类列表
 * Returns: Category[]
 */
Category[] getCategoryList ()
{
    Database db = Database(CONN_STR);
    ResultRange results = db.execute("SELECT * FROM category;");

    Category[] cateList = [];
    if (results.empty)
    {
        return cateList;
    }
    foreach (Row row; results)
    {
        int id = row["id"].as!int;
        string title = row["title"].as!string;
        string visible = row["visible"].as!string;
        int sort = row["sort"].as!int;
        string createTime = row["createTime"].as!string;
        string updateTime = row["updateTime"].as!string;

        Category cate = {
            id: id, title: title, visible: visible, sort: sort, createTime: createTime, updateTime: updateTime
        };
        cateList ~= cate;
    }

    return cateList;
}

/** 
 * 添加分类
 * Params:
 *   category = 待添加的分类
 * Returns: bool
 */
bool addCategory (CategoryDtoForCreate category)
{
    Database db = Database(CONN_STR);
    Statement statement = db.prepare(
        "INSERT INTO category (title, visible, sort, createTime, updateTime) VALUES (:title, :visible, :sort, :createTime, :updateTime);");
    statement.bind(":title", category.title);
    statement.bind(":visible", category.visible);
    statement.bind(":sort", category.sort);
    statement.bind(":createTime", category.createTime);
    statement.bind(":updateTime", category.updateTime);
    try
    {
        statement.execute();
        return db.changes == 1;
    }
    catch (Exception e)
    {
        return false;
    }
}

/** 
 * 更新分类
 * Params:
 *   category = 待编辑的分类
 * Returns: bool
 */
bool updateCategory (CategoryDtoForUpdate category)
{
    Database db = Database(CONN_STR);
    Statement statement = db.prepare(
        "UPDATE category SET title = :title, visible = :visible, sort = :sort, updateTime = :updateTime WHERE id = :id;");
    statement.bind(":title", category.title);
    statement.bind(":visible", category.visible);
    statement.bind(":sort", category.sort);
    statement.bind(":updateTime", category.updateTime);
    statement.bind(":id", category.id);
    try
    {
        ResultRange results = statement.execute();
        return results.empty;
    }
    catch (Exception e)
    {
        return false;
    }
}

/** 
 * 删除分类
 * Params:
 *   id = 待删除的分类id
 * Returns: bool
 */
bool deleteCategory (int id)
{
    Database db = Database(CONN_STR);
    Statement statement = db.prepare("DELETE FROM category WHERE id = :id;");
    statement.bind(":id", id);
    try
    {
        ResultRange results = statement.execute();
        return results.empty;
    }
    catch (Exception e)
    {
        return false;
    }
}

/** 
 * 批量删除分类
 * Params:
 *   ids = 待删除的分类id数组
 * Returns: bool
 */
bool batchDeleteCategory (int[] ids)
{
    import std.array : join;
    import std.algorithm.iteration : map;
    import std.conv : to;

    alias stringize = map!(to!string); // 将int数组转换为string数组

    Database db = Database(CONN_STR);
    string sqlStr1 = `DELETE FROM category WHERE id IN (`;
    string sqlStr2 = `);`;
    string idsStr = stringize(ids).join(",");
    string sqlAll = sqlStr1 ~ idsStr ~ sqlStr2;
    try
    {
        db.begin();
        ResultRange results = db.execute(sqlAll);
        if (db.totalChanges != ids.length)
        {
            db.rollback();
            return false;
        }
        db.commit();

        return results.empty;
    }
    catch (Exception e)
    {
        return false;
    }
}
